import { getServiceProtocolClass } from "./serviceProtocol/serviceProtocol";

/**
 * @name HWSDKService
 * @author
 */

export default class HWSDKService {
  constructor(opts) {
    return new Promise(async (resolve) => {
      const option = {
        key: 0x16,
        moduleName: "hwmService",
        webSocketUri: ""
      };
      Object.assign(option, opts);
      let ServiceProtocolClass = getServiceProtocolClass();
      this.serviceProtocol = await new ServiceProtocolClass(option);
      resolve(this);
    });
  }

  sendMessage({ data, entry, timeoutTime = 35000 }) {
    return new Promise((resolve) => {
      const remoteVars = window.electron.remote.getGlobal("remoteVars");
      console.log(remoteVars);
      let sno = remoteVars.Sno();
      Object.assign(data, { sno });
      let option = {
        resolve,
        data,
        timeoutTime
      };
      this.serviceProtocol.setRspFuncsAndSendMessage(option);
    });
  }
  setNotifyFunc(notify, func) {
    this.serviceProtocol.setNotifyFunc(notify, func);
  }
  /**
   * @api
   * @description 用于启动并且初始化SDK
   * @param exePath string SDK启动路径(路径不能有中文)，SDK压缩包中HwmSdk文件夹所在的绝对路径。如果HwmSdk文件夹的路径为：D:\MyApp\SDK\HwmSdk\，则exePath=D:\MyApp\SDK\。
   * @param siteType int 中国站点:0 亚太站点:1
   */
  setInitSdk(param) {
    const data = {
      cmd: 1,
      description: "hwmService_setInitSdk",
      param: {
        exePath: param.exePath,
        siteType:param.siteType,
        authType:param.authType,
        appId:param.appId
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 设置服务器代理
   * @param server string 服务器地址
   * @param port int 服务器端口号
   * @param account string 代理账号
   * @param password string 代理密码
   */
  setServiceProxy(param) {
    const data = {
      cmd: 2,
      description: "hwmService_setServiceProxy",
      param: {
        server: param.serviceAddress,
        port: param.servicePort,
        account: param.userName,
        password: param.userPassword,
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 设置服务器地址
   * @param serverAddr string 服务器地址
   * @param serverPort int 服务器端口号
   */
  setServiceAddress(param) {
    const data = {
      cmd: 3,
      description: "hwmService_setServiceAddress",
      param: {
        serverAddr: param.serviceAddress,
        serverPort: param.servicePort,
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 登录
   * @param authType string 登录类型
   * @param accountAndPasswordAuthInfo object 帐号密码登录信息
   * @param accountAndPasswordAuthInfo account string 登录帐号
   * @param accountAndPasswordAuthInfo password string 登录密码
   */
  doLogin(param) {
    const data = {
      cmd: 4,
      description: "hwmService_doLogin",
      param: {
        authType: param.authType,
        accountAndPasswordAuthInfo: {
          account: param.account,
          password: param.password
        },
        appIdAuthInfo: {
          thirdUserId: param.thirdUserId,
          userName: param.userName,
          signature: param.signature,
          nonce: param.nonce,
          email: param.email,
          phoneNumber: param.phoneNumber,
          corpId: param.corpId,
          expireTime: param.expireTime,
        }
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 通过会议ID和密码加入已经存在的会议
   * @param confId string 会议ID
   * @param password string 会议密码
   * @param name string 会议昵称
   */
  joinConfByID(param) {
    const data = {
      cmd: 5,
      description: "hwmService_joinConfByID",
      param: {
        confId: param.confId,
        password: param.password,
        name: param.name ? param.name : ""
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 用于退出SDK
   */
  exitSdk() {
    const data = {
      cmd: 6,
      description: "hwmService_exitSdk",
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 用于创建即时会议
   * @param subject string 会议主题
   * @param mediaType string 会议类型
   * @param needPassword bool 是否需要会议接入码
   * @param allowRecord bool 是否允许录制
   * @param attendees object 与会者信息
   * @param attendees name 会场名
   * @param attendees number 号码
   */
  createConf(param) {
    const data = {
      cmd: 7,
      description: "hwmService_joinConfByID",
      param: {
        subject: param.subject,
        mediaType: param.mediaType,
        needPassword: param.needPassword,
        allowRecord: param.allowRecord,
        attendees: param.attendees
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 用于在会议中添加与会者
   */
  addAttendees(param) {
    const data = {
      cmd: 8,
      description: "hwmService_addAttendees",
      param: {
        attendees: param.attendees
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 用于注销已登录的华为云会议帐号
   */
  logout() {
    const data = {
      cmd: 9,
      description: "hwmService_logout"
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 用于自定义会议界面
   */
  setConfig(param) {
    const data = {
      cmd: 10,
      description: "hwmService_setConfig",
      param: {
        frame: param.frame
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 预约会议
   * @param subject string 会议主题
   * @param startTime int 会议开始时间，10位utc时间戳
   * @param duration int 会议时长(minute)
   * @param confType string 会议类型
   * @param isNeedConfPwd bool 是否需要密码
   * @param isRecordOn bool 是否允许录制
   * @param timeZone int 时区码
   * @param vmrFlag bool 会议是否使用个人会议ID
   * @param vmrId string 个人会议ID
   * @param joinConfRestrictionType int 允许入会范围限制 0：所有用户；1：非匿名用户；2：企业内用户；3：被邀请用户
   * @param isSmsOn bool 发送短信通知开关，可选参数，企业配置里面需要开启，否则不生效
   * @param isMailOn bool 发送邮件通知开关，可选参数
   * @param isEmailCalendarOn bool 发送邮件日历通知开关，可选参数
   * @param attendees object 与会者信息
   * @param attendees name string 会场名 可选
   * @param attendees number string 号码
   * @param attendees email string 电子邮箱地址 可选
   * @param attendees sms string 短信通知手机号码 可选
   * @param attendees isMute bool 是否闭音
   * @param attendees isAutoInvite bool 是否自动外邀
   * @param attendees role int 角色  0：普通与会者；1：主席
   */
  bookConf(param) {
    const data = {
      cmd: 11,
      description: "hwmService_bookConf",
      param: {
        subject: param.subject,
        startTime: param.startTime,
        duration: param.duration,
        confType: param.confType,
        isNeedConfPwd: param.isNeedConfPwd,
        isRecordOn: param.isRecordOn,
        timeZone: param.timeZone,
        vmrFlag: param.vmrFlag,
        vmrId: param.vmrId,
        joinConfRestrictionType: param.joinConfRestrictionType,
        isSmsOn: param.isSmsOn,
        isMailOn: param.isMailOn,
        isEmailCalendarOn: param.isEmailCalendarOn,
        attendees: param.attendees,
        attendeeLen: param.attendeeLen,
      }
    };
    console.log(data);
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 获取vmr列表
   */
  getVMRInfo() {
    const data = {
      cmd: 12,
      description: "hwmService_getVMRInfo"
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 用于查询会议详情
   * @param confId string 会议id
   */
  getConfInfoById(param) {
    const data = {
      cmd: 13,
      description: "hwmService_getConfInfoById",
      param: {
        confId: param.confId
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 编辑会议
   * @param confId  string 会议id
   * @param subject string 会议主题
   * @param startTime int 会议开始时间，10位utc时间戳
   * @param duration int 会议时长(minute)
   * @param confType string 会议类型
   * @param isNeedConfPwd bool 是否需要密码
   * @param isRecordOn bool 是否允许录制
   * @param timeZone int 时区码
   * @param vmrFlag bool 会议是否使用个人会议ID
   * @param vmrId string 个人会议ID
   * @param joinConfRestrictionType int 允许入会范围限制 0：所有用户；1：非匿名用户；2：企业内用户；3：被邀请用户
   * @param isSmsOn bool 发送短信通知开关，可选参数，企业配置里面需要开启，否则不生效
   * @param isMailOn bool 发送邮件通知开关，可选参数
   * @param isEmailCalendarOn bool 发送邮件日历通知开关，可选参数
   * @param attendees object 与会者信息
   * @param attendees name string 会场名 可选
   * @param attendees number string 号码
   * @param attendees email string 电子邮箱地址 可选
   * @param attendees sms string 短信通知手机号码 可选
   * @param attendees isMute bool 是否闭音
   * @param attendees isAutoInvite bool 是否自动外邀
   * @param attendees role int 角色  0：普通与会者；1：主席
   */
  editConf(param) {
    const data = {
      cmd: 14,
      description: "hwmService_editConf",
      param: {
        confId: param.confId,
        subject: param.subject,
        startTime: param.startTime,
        duration: param.duration,
        confType: param.confType,
        isNeedConfPwd: param.isNeedConfPwd,
        isRecordOn: param.isRecordOn,
        timeZone: param.timeZone,
        vmrFlag: param.vmrFlag,
        vmrId: param.vmrId,
        joinConfRestrictionType: param.joinConfRestrictionType,
        isSmsOn: param.isSmsOn,
        isMailOn: param.isMailOn,
        isEmailCalendarOn: param.isEmailCalendarOn,
        attendees: param.attendees,
        attendeeLen: param.attendeeLen,
      }
    };
    console.log(data);
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 用于取消会议
   */
  cancelConfById(param) {
    const data = {
      cmd: 15,
      description: "hwmService_cancelConfById",
      param: {
        confId: param.confId
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 清除所有会议界面相关的配置，恢复默认状态
   */
  cleanConfig() {
    const data = {
      cmd: 16,
      description: "hwmService_cleanConfig"
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description 该接口用于设置会场名。
   */
  renameSelf(param) {
    const data = {
      cmd: 18,
      description: "hwmService_RenameSelf",
      param: {
        renameSelf: param.renameSelf
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description
   */
  changeVmrInfo(param) {
    const data = {
      cmd: 19,
      description: "hwmService_ChangeVmrInfo",
      param: {
        vmrId: param.vmrId,
        vmrName: param.vmrName,
        guestPwd: param.guestPwd,
        chairmanPwd: param.chairmanPwd
      }
    };
    return this.sendMessage({ data });
  }
  /**
   * @api
   * @description
   */
  joinConfByLink(param) {
    const data = {
      cmd: 20,
      description: "hwmService_joinConfByLink",
      param: {
        siteUrl: param.siteUrl,
        name: param.name,
        random: param.random
      }
    };
    return this.sendMessage({ data });
  }
}
